package com.wswy.wzcx.ui.fragment

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.che.libcommon.utils.ToastUtils
import com.wswy.wzcx.R
import com.wswy.wzcx.api.Api
import com.wswy.wzcx.api.ConfigsKt
import com.wswy.wzcx.api.ConfigsKt.H5_PRIVACY_POLICY
import com.wswy.wzcx.model.DataCenter
import com.wswy.wzcx.model.home.RouterUtils
import com.wswy.wzcx.module.ChannelManager
import com.wswy.wzcx.module.GlobalConfigManager
import com.wswy.wzcx.module.base.ApiCallback
import com.wswy.wzcx.module.base.CBaseFragment
import com.wswy.wzcx.module.base.onClick
import com.wswy.wzcx.module.base.onResult
import com.wswy.wzcx.ui.dialog.Dialogs
import com.wswy.wzcx.ui.main.MainActivity
import com.wswy.wzcx.ui.other.DialogFactory
import com.wswy.wzcx.ui.user.LoginHolderViewModel
import com.wswy.wzcx.utils.Tools
import kotlinx.android.synthetic.main.fragment_privacy.*


class PrivacyFragment : CBaseFragment() {
    lateinit var loginHolderViewModel: LoginHolderViewModel


    override fun getContentLayoutId(): Int {
        return R.layout.fragment_privacy
    }

    override fun getFragmentTitle(): String {
        return "隐私"
    }

    private var firstStatus = false

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        loginHolderViewModel = provideViewModelFromActivity(LoginHolderViewModel::class.java)

        rl_privacy_policy.onClick {
            if (TextUtils.equals(ChannelManager.getChannel(), "huawei")) {
                RouterUtils.startWeb(it.context, ConfigsKt.H5_PRIVACY_POLICY + "?platform=jtzx")
            } else {
                RouterUtils.startWeb(it.context, ConfigsKt.H5_PRIVACY_POLICY)
            }
        }

        val conf = GlobalConfigManager.getInstance().sysConf.syLogout;
        if (TextUtils.equals(conf?.value, "1")) {
            if (DataCenter.get().hasLogin()) {
                tv_account_logout.visibility = View.VISIBLE
            } else {
                tv_account_logout.visibility = View.GONE
            }
        }

        tv_account_logout.onClick {
            DialogFactory.create(it.context, R.string.account_logout, "确定注销账户吗？", R.string.ok, R.string.cancel) { dialog, which -> accountLogout() }.show()
        }

        //却换
        rl_subscribe.onClick {
            val tmp = !switch_subscribe_notify.isChecked
            if (tmp) {
                showNotifyDialog()
            } else {
                switch_subscribe_notify.isChecked = tmp
                Tools.getSp().putBoolean(DataCenter.KEY_SUBSCRIBE_NOTIFY, tmp)
            }
        }

        switch_subscribe_notify.isChecked = Tools.getSp().getBoolean(DataCenter.KEY_SUBSCRIBE_NOTIFY, false)

        firstStatus = switch_subscribe_notify.isChecked
    }

    private fun showNotifyDialog() {
        Dialogs.showSubscribePrivacy(requireContext()) {
            switch_subscribe_notify?.runCatching {
                isChecked = !isChecked
                if (isChecked) {
                    Tools.getSp().putBoolean(DataCenter.KEY_SUBSCRIBE_NOTIFY, true)
                }

            }
        }
    }

    //用户注销账户
    private fun accountLogout() {
        loginHolderViewModel.addRun(Api.get().submitAccountLogout().onResult({
            ToastUtils.showText("账号注销成功")
            val logoutIntent = Intent(requireContext(), MainActivity::class.java)
            startActivity(logoutIntent)
            finish()
        }, {
            ApiCallback.toastErrorMsg(it)
        }))
    }


    override fun onDestroy() {
        super.onDestroy()
        val newStatus = Tools.getSp().getBoolean(DataCenter.KEY_SUBSCRIBE_NOTIFY, false)
        if (firstStatus != newStatus) {
            saveSettings(newStatus)
        }
    }


    private companion object {
        fun saveSettings(status: Boolean) {
            if (DataCenter.get().hasLogin()) {
                Api.get().subscribeNotify("violation", status).subscribe { t1, t2 ->

                }
            }
        }

    }

}